package com.vue.vuedemo.Controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vue.vuedemo.pojo.Persons;
import com.vue.vuedemo.pojo.Personsplayers;
import com.vue.vuedemo.service.PersonService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
/*
    因为jsonplugin用的是java的内审机制.hibernate会给被管理的pojo加入一个hibernateLazyInitializer属性,
  jsonplugin会把hibernateLazyInitializer也拿出来操作,并读取里面一个不能被反射操作的属性就
 产生了这个异常.  
 */
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@CrossOrigin(origins = {"http://localhost:8080"}) //当前网站向另一个网站请求数据就是跨域
@Controller
public class PersonController {

    @Resource
    private PersonService personService;

    //查询所有人员
    @GetMapping(value = "findAll")
    public List<Persons> findAll(HttpServletResponse response, HttpServletRequest request){
        List<Persons> list =  personService.findAll();
        return list;
    }

    public Integer num;

    //登录验证
    @GetMapping(value = "login")
    public Integer login(String username,String password,
                         HttpServletResponse response,
                         HttpServletRequest request,
                         HttpSession session){
        Persons persons = personService.login(username, password);
        try {
            Integer userId = persons.getId();
            num = userId; //没有查询到数据
        }catch (Exception ex){
            num = 0; //查询到了数据
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        return num;
    }

    //根据id查询人员
    @GetMapping(value = "idFindAll")
    public Persons IdfindAll(Integer id,HttpSession session){
//        id =(Integer) session.getAttribute("userId");
//        id = 1;
        Persons persons =  personService.idFindAll(id);
        return persons;
    }

    //人员添加
    @PostMapping(value = "personsAdd")
    @ResponseBody
    public Integer add(@RequestBody Persons  persons){
        System.out.print(persons.getUsername());
        if(personService.usernameFindAll(persons.getUsername()) == null ||
                personService.usernameFindAll(persons.getUsername()).equals("")){
            try {

                personService.add(persons);
                num = 1; //成功
            }catch (Exception ex){
                num = 2; //失败
            }
        }else{
            num = 3;
        }
        return num;
    }

    //人员删除
    @GetMapping(value = "personsDel")
    public Integer del(Integer id){
        try {
            personService.del(id);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    //人员修改密码
    @GetMapping(value = "personsUpdate")
    public Integer personsUpdate(String password,Integer id, HttpSession session){
        //        id =(Integer) session.getAttribute("userId");
        System.out.print(password);
//        id = 1;
        try {
            personService.updatePassword(password, id);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }


    @PostMapping(value = "tuanDuiAdd")
    @ResponseBody
    public Integer tuanDuiAdd(@RequestBody Persons  persons){
        System.out.print(persons.getUsername());
        if(personService.usernameFindAll(persons.getUsername()) == null ||
                personService.usernameFindAll(persons.getUsername()).equals("")){
            try {

                personService.add(persons);
                num = 1; //成功
            }catch (Exception ex){
                num = 2; //失败
            }
        }else{
            num = 3;
        }
        return num;
    }


    //管理员登录验证
    @GetMapping(value = "managerLogin")
    public Integer managerLogin(String username,String password,
                         HttpServletResponse response,
                         HttpServletRequest request,
                         HttpSession session){
        Persons persons = personService.ManagerLogin(username, password);
        if(!persons.getType().equals("管理员")){
            num = 0;
        }else{
            try {
                Integer userId = persons.getId();
                num = userId; //没有查询到数据
            }catch (Exception ex){
                num = 0; //查询到了数据
            }
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        return num;
    }

    //根据好友表反查用户表
    @GetMapping(value = "fUserIdFindAll")
    public List<Persons> fUserIdFindAll(Integer fUserId,HttpSession session){
        List<Persons> personsFindAll =  personService.FirendPersonsFindAll(fUserId);
        return personsFindAll;
    }

}
